package com.sc.nft.dao;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.nft.entity.CurrentAffairsType;
import com.sc.nft.entity.dto.CurrentAffairsTypeDTO;
import com.sc.nft.entity.vo.CurrentAffairsTypeVo;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.mapper.CurrentAffairsTypeMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 资讯类型表(sc_current_affairs_type)数据DAO
 *
 * @author zhangchao
 * @description 由 Mybatisplus Code Generator 创建
 * @since 2024-03-06 17:16:12
 */
@Slf4j
@Repository
public class CurrentAffairsTypeDao extends ServiceImpl<CurrentAffairsTypeMapper, CurrentAffairsType> {

    /**
     * 获取资讯类型分页数据
     *
     * @param currentAffairsTypeDTO
     * @return
     */
    public Page<CurrentAffairsTypeVo> currentAffairsTypeVoPage(CurrentAffairsTypeDTO currentAffairsTypeDTO) {
        return baseMapper.currentAffairsTypeVoPage(currentAffairsTypeDTO.toMp(), currentAffairsTypeDTO.getId());
    }

    /**
     * 更新资讯类型数据
     *
     * @param currentAffairsType
     */
    public void currentAffairsTypeUpdate(CurrentAffairsType currentAffairsType) {
        LambdaUpdateWrapper<CurrentAffairsType> wrapper = Wrappers.lambdaUpdate();
        wrapper.set(CurrentAffairsType::getCurrentAffairsType,currentAffairsType.getCurrentAffairsType());
        wrapper.set(CurrentAffairsType::getSort,currentAffairsType.getSort());
        wrapper.eq(CurrentAffairsType::getId, currentAffairsType.getId());
        Assert.isTrue(update(wrapper),()->new GlobalRunTimeException("更新失败！"));
    }

    /**
     * 删除资讯类型数据
     *
     * @param id
     * @return
     */
    public Boolean currentAffairsTypeDelete(Long id) {
        LambdaUpdateWrapper<CurrentAffairsType> lambdaUpdateWrapper = Wrappers.lambdaUpdate();
        lambdaUpdateWrapper.eq(CurrentAffairsType::getId, id);
        return remove(lambdaUpdateWrapper);
    }

    /**
     * 启用/禁用资讯类型
     *
     * @param id
     * @param isOpen
     * @return
     */
    public Boolean currentAffairsTypeShow(Long id, Boolean isOpen) {
        LambdaUpdateWrapper<CurrentAffairsType> lambdaUpdateWrapper = Wrappers.lambdaUpdate();
        lambdaUpdateWrapper.eq(CurrentAffairsType::getId, id);
        lambdaUpdateWrapper.set(CurrentAffairsType::getIsOpen, isOpen);
        return update(lambdaUpdateWrapper);
    }

    /**
     * 根据类型名称查询是否有一样的名称
     *
     * @param currentAffairsType
     * @return
     */
    public Integer checkModuleNameAdd(String currentAffairsType) {
        return baseMapper.checkModuleNameAdd(currentAffairsType);
    }

    /**
     * 根据类型名称查询是否有一样的名称，排除自己
     *
     * @param currentAffairsType
     * @param id
     * @return
     */
    public Integer checkModuleNameUpdate(String currentAffairsType, Long id) {
        return baseMapper.checkModuleNameUpdate(currentAffairsType, id);
    }

    /**
     * 获取所有资讯类型集合
     *
     * @return
     */
    public List<CurrentAffairsTypeVo> getCurrentAffairsTypeVo() {
        return baseMapper.getCurrentAffairsTypeVo();
    }

    /**
     * 根据Id获取资讯类型
     *
     * @param id
     * @return
     */
    public CurrentAffairsType getCurrentAffairsTypeById(Long id) {
        return baseMapper.getCurrentAffairsTypeById(id);
    }

    /**
     * 获取开启使用的资讯类型集合
     *
     * @return
     */
    public List<CurrentAffairsTypeVo> getCurrentAffairsTypeVoIsOpen() {
        return baseMapper.getCurrentAffairsTypeVoIsOpen();
    }
}